﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Symmetry.PluginSupport;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Reflection;
using Windows.UI.Popups;
using System.Linq.Expressions;
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Symmetry.Plugins.Osiris
{
        /// <summary>
        /// The idea here is to have a simple quick way to set grid attached properties on the target object.
        /// -Main candidates are the row and column properties but also rowspan, colspan
        /// </summary>
    public sealed partial class GridManager : UserControl, ICustomEditor
    {
        public GridManager()
        {
            this.InitializeComponent();
        }

        public void SetObject(object target)
        {
            if (target == null) return;
            DataContext = target.ToString();
            if (!(target is FrameworkElement)) return;                        
            tbRow.Text = Grid.GetRow(target as FrameworkElement).ToString();
            tbCol.Text = Grid.GetColumn(target as FrameworkElement).ToString();

            _dcFe = target as FrameworkElement;
        }

        FrameworkElement _dcFe = null; //Local fe.

        public string GetDescription()
        {
            return "Provides resources for managing a grids attached properties.";
        }

        public string GetFriendlyName()
        {
            return "Grid Manager";
        }


        public string Description
        {
            get
            {
                return "Provides various functionality to help you manage your grids (including grid attached properties).";
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string FriendlyName
        {
            get
            {
                return "Grid Manager";
            }
            set
            {
                throw new NotImplementedException();
            }
        }


        public string UniqueID
        {
            get
            {
                return "C1C9B2CF-42C4-418F-BDF2-45B9A1C3ED2F";
            }
        }

        private void tbCol_TextChanged(object sender, TextChangedEventArgs e)
        {
            SetColOnTargetFromText(tbCol.Text);
        }

        private void tbRow_TextChanged(object sender, TextChangedEventArgs e)
        {
            SetRowOnTargetFromText(tbRow.Text);
        }


        private void SetColOnTargetFromText(string p)
        {
            int newCol;
            if (int.TryParse(p, out newCol))
            {
                if (_dcFe == null) return;

                Grid.SetColumn(_dcFe, newCol);
            }            
        }
        
        
        private void SetRowOnTargetFromText(string p)
        {
            int newCol;
            if (int.TryParse(p, out newCol))
            {
                if (_dcFe == null) return;

                Grid.SetRow(_dcFe, newCol);
            }                        
        }
    
    }
}
